package com.pubinfo.passbook.common.model.ruler;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSourceArtificial;
import com.pubinfo.passbook.common.model.dto.witeoff.Artificial;
import lombok.Data;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.RoundingMode;
import java.util.List;

@Data
@OnceAbsoluteMerge(firstRowIndex = 5, lastRowIndex = 6, firstColumnIndex = 1, lastColumnIndex = 2)
public class ReimbursementExcel
{
    @ExcelProperty("票据id")
    @ColumnWidth(40)
    private String invoiceId;
    @ExcelProperty("票据代码")
    @ColumnWidth(20)
    private String invoiceCode;
    @ExcelProperty("票据号码")
    @ColumnWidth(20)
    private String invoiceNumber;
    @ExcelProperty("开票日期")
    @ColumnWidth(20)
    private String IssueDate;
    @ExcelProperty("交款人")
    @ColumnWidth(20)
    private String payerPartyName;
    @ExcelProperty("住院/门诊")
    @ColumnWidth(20)
    private String type;
    @ExcelProperty({"总医疗费","A"})
    @ColumnWidth(20)
    private String totalAmount;
    @ExcelProperty({"个人自费","B"})
    @ColumnWidth(20)
    private String selfpaymentCost;
    @ExcelProperty({"个人自理","C"})
    @ColumnWidth(20)
    private String selfpaymentCare;
    @ExcelProperty({"医保列支费用","D=A-B-C"})
    @ColumnWidth(20)
    private String disbursedExpenses;
    @ExcelProperty({"医疗统筹基金支付","E"})
    @ColumnWidth(20)
    private String fundPayAmount;
    @ExcelProperty({"个人账户-本年支付","F"})
    @ColumnWidth(20)
    private String currPayment;
    @ExcelProperty({"报销金额","G=D-E-F"})
    private String reimbursableSum;

    public ReimbursementExcel() {
    }

    public ReimbursementExcel(String invoiceId) {
        this.invoiceId = invoiceId;
        this.totalAmount = "0";
        this.selfpaymentCost = "0";
        this.selfpaymentCare = "0";
        this.disbursedExpenses = "0";
        this.fundPayAmount = "0";
        this.currPayment = "0";
        this.reimbursableSum = "0";
    }


    public ReimbursementExcel(TPassbook tPassbook)
    {
        this.invoiceCode=tPassbook.getEinvoiceCode();
        this.invoiceId=tPassbook.getEInvoiceId();
        this.invoiceNumber=tPassbook.getEinvoiceNumber();
        this.reimbursableSum=tPassbook.getReimbursableSum().setScale(2, RoundingMode.HALF_UP).toString();
        String xml = tPassbook.getEinvoiceXml();
        Document document = null;
        try {
            document = DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
        }
        Element root = document.getRootElement();
        PayInformation payInformation=new PayInformation(root);
        this.totalAmount=payInformation.getTotalAmount().setScale(2, RoundingMode.HALF_UP).toString();
        this.fundPayAmount=payInformation.getFundPayAmount().setScale(2, RoundingMode.HALF_UP).toString();
        this.currPayment=payInformation.getIndividualAccountCurrPayment().setScale(2, RoundingMode.HALF_UP).toString();
        this.selfpaymentCost=payInformation.getSelfpaymentCost().setScale(2, RoundingMode.HALF_UP).toString();
        this.selfpaymentCare=payInformation.getSelfpaymentCare().setScale(2, RoundingMode.HALF_UP).toString();
        this.disbursedExpenses=payInformation.getTotalAmount().setScale(2, RoundingMode.HALF_UP).subtract(payInformation.getSelfpaymentCost().setScale(2, RoundingMode.HALF_UP)).subtract(payInformation.getSelfpaymentCare().setScale(2, RoundingMode.HALF_UP)).toString();
        this.IssueDate=tPassbook.getIssueDate();
        this.payerPartyName=tPassbook.getPayerPartyName();
        this.type = "330601".equals(tPassbook.getClassCode()) ?"门诊":"住院";
    }
    public ReimbursementExcel(TSourceArtificial artificial)
    {
        this.invoiceId="手工录入";
        this.reimbursableSum=artificial.getApprovedAmount().setScale(2, RoundingMode.HALF_UP).toString();
    }
}
